package cn.youqu.happy.module.youqu.controller.app.userTell;

import cn.youqu.happy.framework.common.pojo.CommonResult;
import cn.youqu.happy.framework.common.pojo.PageResult;
import cn.youqu.happy.framework.security.core.annotations.PreAuthenticated;
import cn.youqu.happy.module.youqu.controller.admin.userTell.vo.UserTellPageReqVO;
import cn.youqu.happy.module.youqu.controller.app.userTell.vo.*;
import cn.youqu.happy.module.youqu.convert.userTell.UserTellConvert;
import cn.youqu.happy.module.youqu.dal.dataobject.userTell.UserTellDO;
import cn.youqu.happy.module.youqu.service.userTell.UserTellService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

import static cn.youqu.happy.framework.common.pojo.CommonResult.success;
import static cn.youqu.happy.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Api(tags = "用户 APP - 用户说说")
@RestController
@RequestMapping("/youqu/user-tell")
@Validated
public class AppUserTellController {
    /**
     * 1.新增 首页推荐
     * 2.新增 查看队友的说说
     */
    @Resource
    private UserTellService userTellService;

    @PostMapping("/publish")
    @ApiOperation("发布用户说说")
    @PreAuthenticated
    public CommonResult<Integer> publishUserTell(@Valid @RequestBody AppUserTellPublishReqVO publishReqVO) {
        Long loginUserId = getLoginUserId();
        return success(userTellService.publishUserTell(publishReqVO, loginUserId));
    }

    @PutMapping("/update")
    @ApiOperation("更新用户说说")
    @PreAuthenticated
    public CommonResult<Boolean> updateUserTell(@Valid @RequestBody AppUserTellUpdateReqVO updateReqVO) {
        Long loginUserId = getLoginUserId();
        userTellService.updateUserTell(updateReqVO, loginUserId);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户说说")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthenticated
    public CommonResult<Boolean> deleteUserTell(@RequestParam("id") Integer id) {
        Long loginUserId = getLoginUserId();
        userTellService.deleteUserTell(id, loginUserId);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得用户说说")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthenticated
    public CommonResult<AppUserTellRespVO> getUserTell(@RequestParam("id") Integer id) {
        Long loginUserId = getLoginUserId();
        UserTellDO userTell = userTellService.getUserTell(id, loginUserId);
        return success(UserTellConvert.INSTANCE.convert0(userTell));
    }

    @GetMapping("/list")
    @ApiOperation("获得用户说说列表")//自己可见
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthenticated
    public CommonResult<List<AppUserTellRespVO>> getUserTellList(@RequestParam("ids") Collection<Integer> ids) {

        Long loginUserId = getLoginUserId();
        List<UserTellDO> list = userTellService.getUserTellList(ids, loginUserId);
        return success(UserTellConvert.INSTANCE.convertList0(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得用户说说分页")//自己可见 1.可以做首页推荐  2.可以查询自己发表的 3.可以与自己相关的（先查出自己的好友在筛选还有发布的帖子）
    @PreAuthenticated
    public CommonResult<PageResult<AppUserTellRespVO>> getUserTellPage(@Valid AppUserTellPageReqVO pageVO) {
        Long loginUserId = getLoginUserId();
        UserTellPageReqVO convert = UserTellConvert.INSTANCE.convert(pageVO);
        PageResult<UserTellDO> pageResult = userTellService.getUserTellPage(convert, loginUserId);
        return success(UserTellConvert.INSTANCE.convertPage0(pageResult));
    }

    @GetMapping("/square-page")
    @ApiOperation("获得用户说说广场分页")
    @PreAuthenticated
    public CommonResult<PageResult<AppUserTellRespVO>> getUserTellSquarePage(@Valid AppUserTellSquarePageReqVO pageVO) {
        Long loginUserId = getLoginUserId();
        UserTellPageReqVO convert = UserTellConvert.INSTANCE.convert(pageVO);
        PageResult<UserTellDO> pageResult = userTellService.getUserTellSquarePage(convert, loginUserId);
        return success(UserTellConvert.INSTANCE.convertPage0(pageResult));
    }

    @GetMapping("/partner-page")
    @ApiOperation("获得用户说说队友分页")
    @PreAuthenticated
    public CommonResult<PageResult<AppUserTellRespVO>> getUserTellPartnerPage(@Valid AppUserTellSquarePageReqVO pageVO) {
        Long loginUserId = getLoginUserId();
        UserTellPageReqVO convert = UserTellConvert.INSTANCE.convert(pageVO);
        PageResult<UserTellDO> pageResult = userTellService.getUserTellPartnerPage(convert, loginUserId);
        return success(UserTellConvert.INSTANCE.convertPage0(pageResult));
    }

}
